using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using OurMediaPlayer.Logic.Filters;
using OurMediaPlayer.Logic;
using OurMediaPlayer.View;

namespace OurMediaPlayer
{
    public partial class FiltersForm : SliderForm
    {
        public MainForm mainForm;

        public enum FilterDefinitionState
        {
            FilterAreaChoosen,
            FilterTypeChoosen,
            AreaOverVideoChoosen,
            None
        }

        public enum FilterArea
        {
            Partial,
            Complete,
            None
        }

        public enum FilterType
        {
            Negative,
            Blur,
            Sharpen,
            EdgesDetection,
            Diferences,
            None
        }

        public FilterDefinitionState currentState = FilterDefinitionState.None;
        public FilterArea currentFilterArea = FilterArea.None;
        public FilterType currentFilterType = FilterType.None;

        public FiltersForm()
        {
            InitializeComponent();
        }

        public FiltersForm(Form owner, float fStep):base(owner,fStep)
        {
            this.mainForm = (MainForm)owner;
            this.SlideDirection = ESlideDirection.Right;
            InitializeComponent();
        }

        private void TurnFalseAllRadioButtons()
        {
            this.diferencasRadioButton.Checked = false;
            this.negativoRadioButton.Checked = false;
            this.contornosRadioButton.Checked = false;
            this.passaAltoRadioButton.Checked = false;
            this.passaBaixoRadioButton.Checked = false;
        }

        private void negativoRadioButton_Click(object sender, EventArgs e)
        {
            this.currentState = FilterDefinitionState.FilterTypeChoosen;
            this.currentFilterType = FilterType.Negative;

            this.TurnFalseAllRadioButtons();
            this.negativoRadioButton.Checked = true;

            NegativoFilter nf = new NegativoFilter();
            this.mainForm.filtersManager.currentFilter = nf;

            if (this.currentFilterArea == FilterArea.Partial)
            {
                this.noteGroup.Visible = true;
                this.ClearButton.Visible = true;
                nf.Complete = false;
            }
            else
            {               
                nf.Complete = true;
                nf.Active = true;
            }
        }

        private void passaBaixoRadioButton_Click(object sender, EventArgs e)
        {
            this.currentState = FilterDefinitionState.FilterTypeChoosen;
            this.currentFilterType = FilterType.Blur;

            this.TurnFalseAllRadioButtons();
            this.passaBaixoRadioButton.Checked = true;

            PassaBaixoFilter pbf = new PassaBaixoFilter();
            this.mainForm.filtersManager.currentFilter = pbf;

            if (this.currentFilterArea == FilterArea.Partial)
            {
                this.noteGroup.Visible = true;
                this.ClearButton.Visible = true;
                pbf.Complete = false;
            }
            else
            {
                pbf.Complete = true;
                pbf.Active = true;
            }
        }

        private void passaAltoRadioButton_Click(object sender, EventArgs e)
        {
            this.currentState = FilterDefinitionState.FilterTypeChoosen;
            this.currentFilterType = FilterType.Sharpen;

            this.TurnFalseAllRadioButtons();
            this.passaAltoRadioButton.Checked = true;

            PassaAltoFilter paf = new PassaAltoFilter();
            this.mainForm.filtersManager.currentFilter = paf;

            if (this.currentFilterArea == FilterArea.Partial)
            {
                this.noteGroup.Visible = true;
                this.ClearButton.Visible = true;
                paf.Complete = false;
            }
            else
            {
                paf.Complete = true;
                paf.Active = true;
            }
        }

        private void contornosRadioButton_Click(object sender, EventArgs e)
        {
            this.currentState = FilterDefinitionState.FilterTypeChoosen;
            this.currentFilterType = FilterType.EdgesDetection;

            this.TurnFalseAllRadioButtons();
            this.contornosRadioButton.Checked = true;

            ContornosFilter cf = new ContornosFilter();
            this.mainForm.filtersManager.currentFilter = cf;

            if (this.currentFilterArea == FilterArea.Partial)
            {
                this.noteGroup.Visible = true;
                this.ClearButton.Visible = true;
                cf.Complete = false;
            }
            else
            {
                cf.Complete = true;
                cf.Active = true;
            }
        }

        private void diferencasRadioButton_Click(object sender, EventArgs e)
        {
            this.currentState = FilterDefinitionState.FilterTypeChoosen;
            this.currentFilterType = FilterType.Diferences;

            this.TurnFalseAllRadioButtons();
            this.diferencasRadioButton.Checked = true;

            DiferencasFilter df = new DiferencasFilter();
            this.mainForm.filtersManager.currentFilter = df;

            if (this.currentFilterArea == FilterArea.Partial)
            {
                this.noteGroup.Visible = true;
                this.ClearButton.Visible = true;
                df.Complete = false;
            }
            else
            {
                df.Complete = true;
                df.Active = true;
            }
        }

        private void completeButton_Click(object sender, EventArgs e)
        {
            this.ClearButton_Click(null, null);
            this.currentState = FilterDefinitionState.FilterAreaChoosen;
            this.currentFilterArea = FilterArea.Complete;

            this.TurnFalseAllRadioButtons();
            this.filterTypeGroup.Visible = true;
            this.noteGroup.Visible = false;
            this.ClearButton.Visible = true;
        }

        private void partialButton_Click(object sender, EventArgs e)
        {
            this.ClearButton_Click(null, null);
            this.currentState = FilterDefinitionState.FilterAreaChoosen;
            this.currentFilterArea = FilterArea.Partial;

            this.TurnFalseAllRadioButtons();
            this.filterTypeGroup.Visible = true;
            this.ClearButton.Visible = true;
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            this.TurnFalseAllRadioButtons();
            this.filterTypeGroup.Visible = false;
            if(this.currentFilterArea == FilterArea.Partial)
                this.noteGroup.Visible = false;
            this.currentState = FilterDefinitionState.None;
            this.currentFilterArea = FilterArea.None;
            this.currentFilterType = FilterType.None;
            this.mainForm.filtersManager.currentFilter = null;
            this.ClearButton.Visible = false;
        }

        public void EnableFiltering()
        {
            if (this.mainForm.filtersManager.currentFilter != null)
            {
                Filter currentFilter = this.mainForm.filtersManager.currentFilter;
                currentFilter.Xmin = this.mainForm.filterMinX;
                currentFilter.Ymin = this.mainForm.filterMinY;
                currentFilter.Xmax = this.mainForm.filterMaxX;
                currentFilter.Ymax = this.mainForm.filterMaxY;
                currentFilter.Active = true;
            }
        }

        public void ResetFiltersForm()
        {
            this.TurnFalseAllRadioButtons();

            this.filterTypeGroup.Visible = false;
            this.noteGroup.Visible = false;
            this.currentState = FilterDefinitionState.None;
        }
    }
}